<?php

namespace App\Http\Controllers;

use App\Exceptions\KnowledgeExistsException;
use App\Models\UserKnowledge;
use App\Internals\CourseInternal;

class KnowledgeController extends Controller
{
    /*
     * @api post /study-api/knowledge/{knowledgeId} 添加单词到生词本
     * @version 0.0.1
     * @group web
     *
     * @param path int knowledgeId 知识点ID
     * @param body int tagId 标签ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *  }
     * }
     */
    public function store($knowledgeId)
    {
        $this->validate($this->request, [
            'tagId' => 'required|integer'
        ]);
        $tagId = $this->request->input('tagId');
        if (UserKnowledge::ofUserKnowledge($this->userId, $knowledgeId)->exists()) {
            throw new KnowledgeExistsException();
        }
        $userKnowledgeObj = new UserKnowledge();
        $userKnowledgeObj->setUserId($this->userId);
        $userKnowledgeObj->setKnowledgeId($knowledgeId);
        $userKnowledgeObj->setTagId($tagId);
        $userKnowledgeObj->save();
        return $this->sendSuccessJson();
    }

    /*
     * @api delete /study-api/knowledge/{knowledgeId} 移除生词本中到单词
     * @version 0.0.1
     * @group web
     *
     * @param path int knowledgeId 知识点ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *  }
     * }
     */
    public function destroy($knowledgeId)
    {
        UserKnowledge::ofUserKnowledge($this->userId, $knowledgeId)->forceDelete();
        return $this->sendSuccessJson();
    }

    /*
     * @api get /study-api/knowledge/tags 单词分类
     * @version 0.0.1
     * @group web
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *
     *  }
     * }
     */
    public function getTags()
    {
        $tagIds = UserKnowledge::where('user_id', $this->userId)->pluck('tag_id')->toArray();
        $tags = [];
        if ($tagIds) {
            $tags = CourseInternal::getTags($tagIds);
        }
        return $this->sendSuccessJson($tags);
    }

    /*
     * @api get /study-api/tags/{tagId}/knowledge 分类下的单词
     * @version 0.0.1
     * @group web
     *
     * @param path int tagId 分类ID
     * @param query int [page] 页码，默认1
     * @param query int [pageSize] 页数，默认15
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *
     *  }
     * }
     */
    public function getTagKnowledge($tagId)
    {
        $this->validate($this->request, [
            'pageSize' => "integer|max:100|min:5",
            'page' => "integer|min:1",
        ]);
        $page = intval($this->request->input("page", 1));
        $pageSize = intval($this->request->input("pageSize", 15));
        $total = UserKnowledge::ofTagKnowledge($this->userId, $tagId)->count();
        $knowledgeIds = UserKnowledge::ofTagKnowledge($this->userId, $tagId)->orderBy('id', 'desc')->forPage($page, $pageSize)->pluck('knowledge_id')->toArray();
        $items = [];
        if ($knowledgeIds) {
            $items = CourseInternal::getKnowledge($knowledgeIds);
        }
        return $this->sendSuccessJson(['total'=>$total, 'items'=>$items]);
    }
}
